package com.project2.controller;

import com.project2.bean.CommonData;
import com.project2.bean.ResultVo;
import com.project2.bean.bo.CommentListBO;
import com.project2.bean.po.MarketComment;
import com.project2.bean.po.MarketUser;
import com.project2.bean.vo.CommentCountVO;
import com.project2.bean.vo.CommentListVO;
import com.project2.service.WxCommentService;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/15 17:28
 */
@RestController
@RequestMapping("wx/comment")
public class WxCommentController {

    @Autowired
    WxCommentService wxCommentService;
    /**
     * 根据当前的valueId获取到商品的id
     * 这个showType=0就是包含有图和无图的全部评论，showType=1仅包含的是有图的评论
     */
    @RequestMapping("list")
    public ResultVo list(CommentListBO bo){
        CommonData<CommentListVO> commentList = wxCommentService.list(bo);
        return ResultVo.ok(commentList);
    }

    /**
     * 评论总数和有图片评论总数
     * @param valueId
     * @param type
     * @return
     */
    @RequestMapping("count")
    public ResultVo count(Integer valueId,Byte type){
        CommentCountVO commentCountVO= wxCommentService.count(valueId, type);
        return ResultVo.ok(commentCountVO);
    }

    /**
     * 专题评论中有个post方法 专题评论的type为1 说明是专题评论
     * @param marketComment
     * @return
     */
    @RequestMapping("post")
    public ResultVo post(@RequestBody MarketComment marketComment){
        // 获取当前登录的用户
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if(principals==null){
            ResultVo resultVo = new ResultVo();
            resultVo.setErrno(501);
            resultVo.setErrmsg("需要重新登陆");
            return resultVo;
        }
        MarketUser user = (MarketUser) principals.getPrimaryPrincipal();
        MarketComment marketComment1 = wxCommentService.addTopicComment(marketComment,user);
        return ResultVo.ok(marketComment1);

    }
}
